//
//  CTabBar.swift
//  WanZhou
//
//  Created by xhl_dabaoMac on 2021/11/11.
//

import UIKit

public class CTabBar: UIView {

    //背景图片。
    public let BGImageView:UIImageView = {
        let imageView = UIImageView()
        imageView.backgroundColor = .white
        imageView.isUserInteractionEnabled = true
        return imageView
    }()
    public var BGNetImageUrls:String? {
        didSet{
            BGImageView.kf.setImage(with: URL(string: BGNetImageUrls ?? ""))
        }
    }
    
    
    var titles = [String]()//tabbar item 标题
    var normalImages = [UIImage?]()//tabbar item 未选中图片
    var selectImages = [UIImage?]()//tabbar item 选中图片
    var netNormalImageUrls = [String]()  //tabbar item 未选中图片网络图
    var netSelectImageUrls = [String]()  //tabbar item 选中图片网络图

    var selectCButton : CButton?
    var CButtons = [CButton]()

    public var selectIndex : Int {
        didSet{
            if selectIndex >= 0,selectIndex < CButtons.count {
                let button = CButtons[selectIndex]
                if !button.isEqual(selectCButton) {
                    selectCButton?.select = false;
                    selectCButton = button;
                    selectCButton?.select = true
                }
            }
        }
    }
    override init(frame: CGRect) {
        self.selectIndex = 0
        super.init(frame: frame)
    }
    
    public init(frame: CGRect,
                titles:[String],
                normalImages:[UIImage?],
                selectImages:[UIImage?],
                netNormalImageUrls:[String],
                netSelectImageUrls:[String]) {
        
        self.selectIndex = 0
        super.init(frame: frame)
        self.isUserInteractionEnabled = false
        self.titles = titles
        self.normalImages = normalImages
        self.selectImages = selectImages
        self.netNormalImageUrls = netNormalImageUrls
        self.netSelectImageUrls = netSelectImageUrls

        addItems()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    deinit {
        debugPrint("deinit:\(self)")
    }
}

//添加子视图
extension CTabBar{
    func addItems(){
        
        addSubview(BGImageView)
        BGImageView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalToSuperview()
        }
        var temp : CButton?
        CButtons = CButton.getCButtons(titles: titles, normalImage: normalImages,selectImages: selectImages)
        for i in 0 ..< CButtons.count {
            let cbutton = CButtons[i]
            BGImageView.addSubview(cbutton)
            cbutton.snp.makeConstraints { make in
                make.top.equalToSuperview()
                make.height.equalTo(50)
                make.width.equalTo(Int(UIScreen.main.bounds.size.width)/titles.count)
                if let t = temp {
                    make.left.equalTo(t.snp.right)
                    cbutton.select = false
                }else{
                    make.left.equalToSuperview()
                    cbutton.select = true
                    selectCButton = cbutton
                }
            }
            temp = cbutton
        }
    }
}

